<style scoped>

</style>
<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['my:xyk:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['my:xyk:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['my:xyk:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['my:xyk:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="xykList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="信用卡名称" align="center" prop="cardName"/>
      <!--      <el-table-column label="信用卡行名" align="center" prop="cardHang" />-->
      <el-table-column label="总额度" align="center" prop="totalNum"/>
      <el-table-column label="可用额度" align="center" prop="leaveNum" >
        <template slot-scope="scope">
          <span :style="scope.row.yearFee.startsWith('青云')?'color:yellow;font-weight: 600;background:black;padding: 1rem;':'color:black;'">{{scope.row.leaveNum}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-remove"
            @click="handleIncrease(scope.row)"
            v-hasPermi="['my:xyk:remove']"
          ></el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleMinus(scope.row)"
            v-hasPermi="['my:xyk:remove']"
          ></el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['my:xyk:edit']"
          ></el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['my:xyk:remove']"
          ></el-button>
        </template>
      </el-table-column>
      <el-table-column label="年费政策" align="center" prop="yearFee">
        <template slot-scope="scope">
          <el-tooltip placement="right" effect="dark">
            <div slot="content" style="max-width: 200px;font-weight: bolder;font-size: 16px" >{{ scope.row.yearFee}}</div>
            <span>{{ scope.row.yearFee.length > 5 ? scope.row.yearFee.slice(0, 5)
              + '...' :scope.row.yearFee }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="上一次提额时间" align="center" prop="lastUpTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastUpTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提额时间间隔" align="center" prop="upTimeGap"/>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改信用卡对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="信用卡名称" prop="cardName">
          <el-input v-model="form.cardName" placeholder="请输入信用卡名称"/>
        </el-form-item>
        <el-form-item label="信用卡行名" prop="cardHang">
          <el-input v-model="form.cardHang" placeholder="请输入信用卡行名"/>
        </el-form-item>
        <el-form-item label="总额度" prop="totalNum">
          <el-input v-model="form.totalNum" placeholder="请输入总额度"/>
        </el-form-item>
        <el-form-item label="可用额度" prop="leaveNum">
          <el-input v-model="form.leaveNum" placeholder="请输入可用额度"/>
        </el-form-item>
        <el-form-item label="年费政策" prop="yearFee">
          <el-input v-model="form.yearFee" placeholder="请输入年费政策"/>
        </el-form-item>
        <el-form-item label="上一次提额时间" prop="lastUpTime">
          <el-date-picker clearable size="small"
                          v-model="form.lastUpTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择上一次提额时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="提额时间间隔" prop="upTimeGap">
          <el-input v-model="form.upTimeGap" placeholder="请输入提额时间间隔"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 还款 -->
    <el-dialog :title="title" :visible.sync="openIn" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="还款" prop="totalNum">
          <el-input placeholder="金额"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addDeleteLeft(false)">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {addXyk, delXyk, getXyk, listXyk, updateXyk} from "@/api/my/xyk";

  export default {
    name: "Xyk",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: false,
        // 总条数
        total: 0,
        // 信用卡表格数据
        xykList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          cardName: [
            {required: true, message: "信用卡名称不能为空", trigger: "blur"}
          ],
          cardHang: [
            {required: true, message: "信用卡行名不能为空", trigger: "blur"}
          ],
          totalNum: [
            {required: true, message: "总额度不能为空", trigger: "blur"}
          ],
        },
        openIn: false,
        openOut: false
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询信用卡列表 */
      getList() {
        this.loading = true;
        listXyk(this.queryParams).then(response => {
          this.xykList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          cardName: null,
          cardHang: null,
          totalNum: null,
          leaveNum: null,
          yearFee: null,
          lastUpTime: null,
          upTimeGap: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加信用卡";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getXyk(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改信用卡";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateXyk(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addXyk(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      handleIncrease(row) {
        this.form.id = row.id;
        this.title = row.cardName;
        this.openIn = true;
      },
      handleMinus(row) {
        this.form.id = row.id;
        this.title = row.cardName;
        this.openOut = true;
      },
      addDeleteLeft(op) {
        if (this.form.id != null) {
          if (op) {
            updateXyk(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            updateXyk(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        } else {
          this.$modal.msgError("请选择一个卡操作");
        }
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除信用卡编号为"' + ids + '"的数据项？').then(function () {
          return delXyk(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('my/xyk/export', {
          ...this.queryParams
        }, `xyk_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
